package android.project;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Gallerys extends Activity {

	private static final int MENU_ITEM0=0;
	private static final int MENU_ITEM1=0;
	private static final int MENU_ITEM2=0;
	
    private Gallery gallery;
    private ImageView imgView;
    private int image_pos;

    private Intent intentSub1;
    
    private Integer[] Imgid = {
            //R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7
            
    };
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery);

        //intentSub1 = new Intent("android.example.MenuSub.intentMsg");
        
        imgView = (ImageView)findViewById(R.id.ImageView01);	
        imgView.setImageResource(Imgid[0]);
        
         gallery = (Gallery) findViewById(R.id.examplegallery);
         gallery.setAdapter(new AddImgAdp(this));

         gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                imgView.setImageResource(Imgid[position]); 
                image_pos=position;
            }
        });

    }

    public class AddImgAdp extends BaseAdapter {
        int GalItemBg;
        private Context cont;

        public AddImgAdp(Context c) {
            cont = c;
            TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
            GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
            typArray.recycle();
        }

        public int getCount() {
            return Imgid.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imgView = new ImageView(cont);

            imgView.setImageResource(Imgid[position]);
            imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
            imgView.setScaleType(ImageView.ScaleType.FIT_XY);
            imgView.setBackgroundResource(GalItemBg);

            return imgView;
        }
    }
    /*
     * ImageView
     * 
     */
    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(getApplication())
                .inflate(R.menu.menu, menu);
        return(super.onPrepareOptionsMenu(menu));
	}
	public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
           case R.id.Menu1:
               Toast.makeText(this, "Menu 1", Toast.LENGTH_SHORT).show();
               break;
           case R.id.Menu2:
               Toast.makeText(this, "Menu 2", Toast.LENGTH_SHORT).show();
               break;
           case R.id.Menu3:
               Toast.makeText(this, "Menu 3", Toast.LENGTH_SHORT).show();
               break;
           case R.id.submenu1:
               //Toast.makeText(this, "Sub menu1", Toast.LENGTH_SHORT).show();
               //intentSub1.putExtra("songname", et.getText().toString());
               //startActivity(intentSub1);
        	   Toast.makeText(this, "item"+image_pos+" selected",Toast.LENGTH_SHORT).show();
        	   intentSub1 = new Intent("android.example.MenuSub.intentMsg");
        	   String tmp=Integer.toString(image_pos);
        	   intentSub1.putExtra("imageNum", tmp);
               startActivity(intentSub1);
        	   break;
           case R.id.submenu2:
               //Toast.makeText(this, "Sub menu2", Toast.LENGTH_SHORT).show();
               //Intent intent = new Intent(MenuExActivity.this, ShareEmail.class);
               //startActivity(intent);
               break;
       }
       return(super.onOptionsItemSelected(item));
   }


}

